﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using _4T.Data;
using _4T.BLL;

namespace _4T
{
    public partial class ProfileInfoAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["p"] != null)
            {
                string profileCode = Request.QueryString["p"].ToString();
                LoadProfileInfo(profileCode);
                hdfProfileCode.Value = profileCode;
            }
        }

        private void LoadProfileInfo(string profileCode)
        {
            ProfileInfoBLL infoBLL = new ProfileInfoBLL();
            CMS_ProfileInfo info = infoBLL.GetLastProfileInfoByProfileCode(profileCode);
            if (info != null)
            {
                // Load patient info
                PatientInfoBLL patientBLL = new PatientInfoBLL();
                PatientInfo patient = patientBLL.GetPatientInfo(info.ProfileInfoCode);
                if (patient != null)
                {
                    txtPatientName.Text = patient.PatientName;
                    txtDOB.Text = patient.Birthday.ToShortDateString();
                    ddlGender.SelectedValue = patient.Gender;
                    txtIDNumber.Text = patient.IDNumber;
                    txtPermAddr.Text = patient.PermanentAddress;
                    txtTempAddr.Text = patient.TemporaryAddress;
                    txtMobileNumber.Text = patient.MobileNumber;
                    txtPhoneNumber.Text = patient.PhoneNumber;
                    txtRepresentativeName.Text = patient.EmergencyRepresentative;
                    txtRepresentativePhone.Text = patient.RepresentativePhoneNumber;
                    txtLocalContactName.Text = patient.LocalContactPoint;
                    txtLocalContactPhone.Text = patient.LocalContactPointPhoneNumber;
                    txtMedicalStaffName.Text = patient.LocalMedicalStaff;
                    txtMedicalStaffPhone.Text = patient.LocalMedicalStaffPhoneNumber;
                    txtMedicalFacility.Text = patient.MedicalFacility;
                    txtNote.Text = patient.Note;
                }

                // Load medical info
                MedicalInfoBLL medicalBLL = new MedicalInfoBLL();
                MedicalInfo medical = medicalBLL.GetMedicalInfo(info.ProfileInfoCode);
                if (medical != null)
                {
                    txtDiagnosis.Text = medical.Diagnosis;
                    txtMedicalTime.Text = medical.MedicalTime.ToShortDateString();
                    txtTreatmentMethod.Text = medical.TreatmentMethod;
                    txtSituation.Text = medical.Situation;
                    txtSupport.Text = medical.Support;
                }


                // Load info supplier info
                InfoSupplierInfoBLL infoSupplierBLL = new InfoSupplierInfoBLL();
                InfoSupplierInfo supplier = infoSupplierBLL.GetInfoSupplierInfo(info.ProfileInfoCode);
                if (supplier != null)
                {
                    txtInfoSupplierName.Text = supplier.SupplierName;
                    txtInfoSupplierPhone.Text = supplier.PhoneNumber;
                    txtPersonalCode.Text = supplier.PersonalCode;
                    txtEmail.Text = supplier.Email;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string supplierCode = "guestsoncamedia";
            if (Session["User"] != null)
            {
                supplierCode = Session["User"].ToString();
            }
            PatientInfo patient = new PatientInfo(txtPatientName.Text, Convert.ToDateTime(txtDOB.Text),
                ddlGender.SelectedValue, txtIDNumber.Text, txtPermAddr.Text, txtTempAddr.Text,
                txtMobileNumber.Text, txtPhoneNumber.Text, txtRepresentativeName.Text,
                txtRepresentativePhone.Text, txtLocalContactName.Text, txtLocalContactPhone.Text,
                txtMedicalStaffName.Text, txtMedicalStaffPhone.Text, txtMedicalFacility.Text,
                txtNote.Text);
            MedicalInfo medical = new MedicalInfo(txtDiagnosis.Text, Convert.ToDateTime(txtMedicalTime.Text),
                txtTreatmentMethod.Text, txtSituation.Text, txtSupport.Text, null);
            InfoSupplierInfo supplier = new InfoSupplierInfo(txtInfoSupplierName.Text, txtInfoSupplierPhone.Text, txtPersonalCode.Text, txtEmail.Text, null);
            ProfileInfo profileInfo = new ProfileInfo(hdfProfileCode.Value);
            Profile profile = new Profile();
            profile.ProfileInfo = profileInfo;
            profile.PatientInfo = patient;
            profile.MedicalInfo = medical;
            profile.InfoSupplierInfo = supplier;
            ProfileBLL profileBLL = new ProfileBLL();
            bool result = profileBLL.InsertProfileInfo(profile, supplierCode);
            if (result) Response.Redirect("ProfileList_CMS.aspx");
        }
    }
}